/**
 * TODO
 * 
 */
package com.aitongyi.rabbitmq.helloworld;

import com.aitongyi.rabbitmq.Base;
import com.rabbitmq.client.*;
import java.io.IOException;

/**
 *
 * 消费者
 * @author kata
 * 
 */
public class ConsumerJ extends Base{

	private final static String QUEUE_NAME = "hello1";

	public static void main(String[] argv) throws Exception {
		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost(Base.hostname);
		//创建连接;
		Connection connection = factory.newConnection();
		// 通道;
		Channel channel = connection.createChannel();

		channel.queueDeclare(QUEUE_NAME, false, false, true, null);

		System.out.println("C [*] Waiting for messages. To exit press CTRL+C");

		Consumer consumer = new DefaultConsumer(channel) {
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
				String message = new String(body, "UTF-8");
				System.out.println("C [x] Received '" + message + "'");
			}
		};
		channel.basicConsume(QUEUE_NAME, true, consumer);
	}
}
